var qcloud = require('../../vendor/qcloud-weapp-client-sdk/index');
const util = require('../../utils/util.js');

// 引入配置
var config = require('../../config');

// 显示繁忙提示
var showBusy = text => wx.showToast({
    title: text,
    icon: 'loading',
    duration: 10000
});

// 显示成功提示
var showSuccess = text => wx.showToast({
    title: text,
    icon: 'success'
});

// 显示失败提示
var showModel = (title, content) => {
    wx.hideToast();

    wx.showModal({
        title,
        content: JSON.stringify(content),
        showCancel: false
    });
};


Page({
    data: {
        money: "",
        amount: 0,
        fromUserOpenId: '',
        toUserId: '',
        toUserName: '',
        messageId: '',
        toUserAvatarUrl: '',
        donateUrl: config.service.donateUrl,
    },
    onLoad: function (options) {
        // 生命周期函数--监听页面加载
        // 取到 messageid userid selfuserid
        this.data.messageId = options.messageid;
        this.data.toUserId = options.userid;
        this.data.toUserName = options.username;
        this.data.toUserAvatarUrl = options.avatarurl;
        var userInfo = wx.getStorageSync('userInfo');
        // this.data.fromUserId = userIn
        this.data.fromUserOpenId = userInfo.openId;

        this.setData({
            toUserName: this.data.toUserName,
            toUserAvatarUrl: this.data.toUserAvatarUrl
        });
    },
    onReady: function () {
        // 生命周期函数--监听页面初次渲染完成

    },
    onShow: function () {
        // 生命周期函数--监听页面显示

    },
    onHide: function () {
        // 生命周期函数--监听页面隐藏

    },
    onUnload: function () {
        // 生命周期函数--监听页面卸载

    },
    onPullDownRefresh: function () {
        // 页面相关事件处理函数--监听用户下拉动作

    },
    onReachBottom: function () {
        // 页面上拉触底事件的处理函数

    },
    onShareAppMessage: function () {
        // 用户点击右上角分享
        return {
            title: 'title', // 分享标题
            desc: 'desc', // 分享描述
            path: 'path' // 分享路径
        }
    },
    tap1: function () {
        this.input(1);
    },
    tap2: function () {
        this.input(2);
    },
    tap3: function () {
        this.input(3);
    },
    tap4: function () {
        this.input(4);
    },
    tap5: function () {
        this.input(5);
    },
    tap6: function () {
        this.input(6);
    },
    tap7: function () {
        this.input(7);
    },
    tap8: function () {
        this.input(8);
    },
    tap9: function () {
        this.input(9);
    },
    tap0: function () {
        this.input(0);
    },
    tapDot: function () {
        this.input('.');
    },
    tapDel: function () {
        this.input('del');
    },
    input: function (val) {
        var money = this.data.money;
        if (!money) {
            money = '0';
        }
        var index = money.indexOf('.');
        if (val === '.') {
            if (index == -1) {
                money = money + '.';
            }
        } else if (val === 'del') {
            money = money.substring(0, money.length - 1)
        } else {
            if (val === 0 && (money === '0' || money.length == 0)) {
                money = '0';
            } else {
                var floatmoney = parseFloat(money);
                if (floatmoney == 0 && index == -1) {
                    money = '' + val;
                } else {
                    money = money + val;
                }
            }
        }

        var index = money.indexOf('.');

        var amount = parseFloat(money);
        if (index != -1) {
            amount = amount.toFixed(2);
            if (index + 3 < money.length) {
                money = '' + amount;
            }
        }

        this.data.amount = amount;
        this.setData({
            money: money,
            amount: amount
        });
    },
    /**
     * 确定捐赠
     */
    doDonate: function () {
        console.log("do donate");
        var that = this;
        var data = {
            fromUserOpenId: this.data.fromUserOpenId,
            toUserId: this.data.toUserId,
            donateAmount: this.data.amount
        };
        showBusy('提交数据');
        qcloud.request({
            // 要请求的地址
            url: that.data.donateUrl,
            data: data,
            method: 'POST',
            success(result) {
                showSuccess('提交动态成功');
                console.log('request success', result);
            },
            fail(error) {
                showModel('请求失败', error);
                console.log('request fail', error);
            },
            complete() {
                console.log('request complete');
            }
        });
    }
})